import {defineStore} from 'pinia'
import {UserApi} from "@/api/user/index.js";
import {ElMessage} from "element-plus";

export const useUserStore = defineStore('user',{
    state:()=>({
        userList:[],
        pageInfo:{
            page:1,
            size:10,
            total:0,
            totalPage:0
        }
    }),
    actions:{
        async getUserList(params){
            // 确保参数中包含显示已删除用户的选项
            const requestParams = {
                ...params,
                includeDeleted: true,
                withDeleted: true
            };

            const promise = await UserApi.list(requestParams);
            this.userList = promise.data.records

            this.pageInfo.page = promise.data.pageNumber
            this.pageInfo.size = promise.data.pageSize
            this.pageInfo.total = promise.data.totalRow
            this.pageInfo.totalPage = promise.data.totalPage
        },
        //更新用户信息
        async updateUser(user){
            const promise = await UserApi.updateUser(user);
            if(promise.code === 200){
                ElMessage.success(`更新成功`);
                await this.getUserList();
            }else{
                ElMessage.error('更新失败');
            }
            return promise;
        },
        //删除用户
        async deleteUser(userId){
            try {
                const promise = await UserApi.deleteUser({userId: userId});
                if(promise.code === 200){
                    return promise;
                }else{
                    throw new Error(promise.message || '删除失败');
                }
            } catch (error) {
                throw error;
            }
        },


        // 在 src/stores/userStore.js 的 actions 中修改 resetUserPassword 方法
        async resetUserPassword(passwordData) {
            const response = await UserApi.updateUserPasswordByAdmin(passwordData);
            return response;
        },









        // 在 userStore.js 中添加更新密码的方法
        async updatePassword(passwordData) {
            // 调用后端更新密码接口
            const response = await api.updateUserPassword(passwordData);
            return response.data;
        }


    }
})
